package com.fmy.jurisdiction.core.commons.kits;

import com.alibaba.fastjson.JSONObject;
import com.fmy.jurisdiction.core.web.specs.Response;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName RequestTool.java
 * @Description TODO request工具集
 * @createTime 2019年06月24日 14:02:00
 */
public class RequestKit {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    /**
     * 返回参数
     */
    public static void responseWriter(HttpServletResponse response, HttpStatus httpStatus, Response responseWriter) throws IOException {
        response.setStatus(httpStatus.value());
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().println(JSONObject.toJSONString(responseWriter));
    }
}
